package com.qt.socket.bigwork;

import lombok.SneakyThrows;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author zsf
 * @Date 2023/10/6 15:34
 * @Version 1.0
 */
public class MyRunnableRoom  implements Runnable{

    private Socket socket;
    private BufferedReader br;

    public MyRunnableRoom(Socket socket ,BufferedReader br ) {
        this.socket = socket;
        this.br = br;
    }


    @Override
    public void run() {

        try {
            while (true){
                String info = br.readLine();
                if("login".equals(info)){
                    System.out.println("login");
                    checkClientSend(br,socket);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static void sendAll(String message,String username) throws IOException {
        ArrayList<Socket> sockets = ServerRoom.sockets;
        for (Socket s : sockets) {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write(username + " 发送消息： " + message);
            bw.newLine();
            bw.flush();
        }
    }


    private static void checkClientSend(BufferedReader br, Socket socket) throws IOException {

        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("aaa\\user.txt");
            properties.load(fis);
            fis.close();
            String userInfo = br.readLine();
            checkUser(properties,socket,userInfo,br);
    }

    private static void checkUser( Properties properties, Socket socket,String userInfo,BufferedReader br) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String[] split = userInfo.split("=");
        String username = split[0];
        String userPassword = split[1];
        if(properties.containsKey(username)){

            String value = properties.getProperty(username);
            if(value.equals(userPassword)){
                bw.write("1");
                bw.newLine();
                bw.flush();

                sendAll(socket,br,username);




            }else {
                bw.write("3");
                bw.newLine();
                bw.flush();
            }

        }else {
            bw.write("2");
            bw.newLine();
            bw.flush();
        }




    }

    private static void sendAll(Socket socket, BufferedReader br, String username) {
        //用户信息校验成功的话  将socket添加到集合中
        ServerRoom.sockets.add(socket);
        //开启线程去  接收消息群发

        //接受到客户端消息进行群发
        while (true){
            try {
                String message = br.readLine();
                System.out.println(message);
                sendAll(message,username);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


}
